import { tween } from 'cc';
import { v3 } from 'cc';
import { Vec3 } from 'cc';
import { _decorator, Component, Node } from 'cc';


export class PathUtil {
    /**
     * 计算两个点之间的距离
     * @param startPos 起始点
     * @param endPos 结束点
     * @returns 距离
     */
    /** 播放掉落动画(抛物线运动) */
    static setupDropAnimation(node: Node, startPos: Vec3, endPos: Vec3, duration: number = 1, height: number = 1, callback?: Function) {
        const delta = endPos.clone().subtract(startPos);
        tween(node)
            .to(duration, {}, {
                onUpdate: (_, ratio: number) => {
                    // 标准化抛物线公式为： [ y = h \cdot 4 \cdot (t - t^2) ]
                    const currentRatio = ratio;
                    const newX = startPos.x + delta.x * currentRatio;
                    const newZ = startPos.z + delta.z * currentRatio;
                    const baseY = startPos.y + delta.y * currentRatio;
                    const parabolaY = baseY + height * 4 * (currentRatio - currentRatio ** 2);
                    node.position = v3(newX, parabolaY, newZ);
                }
            })
            .call(() => {
                callback?.();
            })
            .start();
    }
}


